package com.example.imagecompressor.dto;

/**
 * DTO representing the result of processing a single image file.
 */
public class ProcessingResult {

    private String originalFilename;
    private Status status;
    private String outputFilename; // Filename on the server, used for download link
    private String finalSize;
    private String errorMessage;

    public enum Status {
        SUCCESS,
        FAILURE
    }

    // Private constructor for builder pattern or static factory methods
    private ProcessingResult() {}

    public static ProcessingResult success(String originalFilename, String outputFilename, long finalSizeBytes) {
        ProcessingResult result = new ProcessingResult();
        result.originalFilename = originalFilename;
        result.status = Status.SUCCESS;
        result.outputFilename = outputFilename;
        result.finalSize = formatFileSize(finalSizeBytes); // Format size for display
        result.errorMessage = null;
        return result;
    }

    public static ProcessingResult failure(String originalFilename, String errorMessage) {
        ProcessingResult result = new ProcessingResult();
        result.originalFilename = originalFilename;
        result.status = Status.FAILURE;
        result.outputFilename = null;
        result.finalSize = "-";
        result.errorMessage = errorMessage;
        return result;
    }

    // --- Getters (Needed for JSON serialization by Spring/Jackson) ---

    public String getOriginalFilename() {
        return originalFilename;
    }

    public Status getStatus() {
        return status;
    }

    public String getOutputFilename() {
        return outputFilename;
    }

    public String getFinalSize() {
        return finalSize;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    // Static helper method copied from ImageProcessingService (consider a Util class)
    private static String formatFileSize(long bytes) {
        long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
        if (absB < 1024) {
            return bytes + " B";
        }
        long value = absB;
        java.text.CharacterIterator ci = new java.text.StringCharacterIterator("KMGTPE");
        for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
            value >>= 10;
            ci.next();
        }
        value *= Long.signum(bytes);
        return String.format("%.1f %ciB", value / 1024.0, ci.current());
    }
} 